package com.livefilms.main;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.log4j.Logger;


public class Main {
	
	private static Logger log = Logger.getLogger(Main.class);
	
	public static void main(String[] args) throws UnsupportedEncodingException {
		
		int firstArg = 1;
		String url = "http://www.cb01.eu/";
		if (args.length > 0) {
		    try {
		        firstArg = Integer.parseInt(args[0]);
		        url = args[1];
		    } catch (NumberFormatException e) {
		    	log.error("Argument" + args[0] + " deve essere un intero!.");
		        System.exit(1);
		    }
		}
		
		log.info("SCAN PAGE: "+firstArg+" Url:"+url);
		
		HttpProcess process = new HttpProcess();
		HttpDocument document = new HttpDocument();
		HttpUtility utility = new HttpUtility();
		
		for(int page=1; page <= firstArg; page++){
		
			List<ObjectFilm> listFilmPage = document.getPage(page,url);
			
			for(ObjectFilm objfilm: listFilmPage){
				CineModel film = new CineModel();
				film.setUrlPage(objfilm.getLink());
				film.setTitolo(utility.getTitolo(objfilm.getTitolo()));
			
				film.setDescrizione(utility.getDescrizione(objfilm.getDescrizione()));
				film.setAnno(utility.getAnno(objfilm.getTitolo()));
				film.setDurata(utility.getDurata(objfilm.getInfo()));
				film.setPaese(utility.getPaese(objfilm.getInfo()));
				film.setBn(utility.isBn(objfilm.getTitolo()));
				film.setSub(utility.isSub(objfilm.getTitolo()));
				film.setTipologia(utility.getTipologia(objfilm.getInfo()));
				film.setLocandina(objfilm.getLocandina());
				film.setId(utility.generateCode(film));
				film.setHost(document.getListVideo(film.getUrlPage(), film.getId()));
				film.setDataUscita(utility.generateData(document.getDataVideo()));
			
				
				process.ingestion(film);
			//	break;
			}
		}

	}
}